package com.ld.dao;

import com.ld.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

@Repository
public class UserDao {

    @Autowired
    private MongoTemplate mongoTemplate;

    public void insert(User user){
        mongoTemplate.insert(user);
    }

    public void update(User user){
        Query query = Query.query(Criteria.where("id").is(user.getId()));
        Update update = new Update().set("age",user.getAge());
        mongoTemplate.updateFirst(query,update,User.class);
    }

    public void deleteById(Long id) {
        Query query=new Query(Criteria.where("id").is(id));
        mongoTemplate.remove(query,User.class);
    }

    public User find(Long id){
        Query query = new Query(Criteria.where("id").is(id));
       return mongoTemplate.findOne(query,User.class);
    }
}
